package com.order.itemcategoryproject.controller;

import com.order.itemcategoryproject.pojo.VO.dArcItemVo;
import com.order.itemcategoryproject.pojo.dArcItemPicFk;
import com.order.itemcategoryproject.pojo.dQiyeSendMsg;
import com.order.itemcategoryproject.service.dArcItemPicFkService;
import com.order.itemcategoryproject.service.dArcItemPicService;
import com.order.itemcategoryproject.service.dArcItemService;
import com.order.itemcategoryproject.service.dQiyeSendMsgService;
import com.order.itemcategoryproject.utils.AjaxResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.List;

@RestController
@RequestMapping("/fk")
public class dArcItemPicFkController {

    @Autowired
    private dArcItemPicFkService dArcItemPicFkService;

    @Autowired
    private dArcItemService dArcItemService;

    @Autowired
    private dQiyeSendMsgService dQiyeSendMsgService;

    @PostMapping("/patchInsertErrorImg")
    public AjaxResult patchInsertErrorImgFK(@RequestBody List<dArcItemPicFk> dArcItemPicFks) {
        if (ObjectUtils.isEmpty(dArcItemPicFks)) {
            return AjaxResult.error("为获取到当前商品图片!!");
        }
        int effectLine = 0;
        // 如果当前商品反馈的图片不为空，那么就可以直接进行添加啦
        for (dArcItemPicFk dArcItemPicFk : dArcItemPicFks) {
            effectLine = dArcItemPicFkService.insertImgFkOpinion(dArcItemPicFk);
        }

        if (effectLine > 0) {
            return AjaxResult.success("添加成功.");
        } else {
            return AjaxResult.error("未添加成功.");
        }
    }


    /**
     * 根据商品编号以及客户代码获取当前反馈图片
     *
     * @param itemId
     * @param cusCode
     * @return
     */
    @GetMapping("/selectErrorImgs/{itemId}/{cusCode}")
    public AjaxResult getAllErrorImgFkByItemIdAndcusCode(@PathVariable String itemId, @PathVariable String cusCode) {
        if (StringUtils.isBlank(itemId) || StringUtils.isBlank(cusCode)) {
            return AjaxResult.error("参数未被定义.");
        }

        // 如果参数存在，那么就获取当前商品有没有添加反馈的图片
        List<dArcItemPicFk> dArcItemPicFks = dArcItemPicFkService.selectAllImgFkByCusIdAndItemId(cusCode, itemId);
        if (ObjectUtils.isEmpty(dArcItemPicFks)) {
            return AjaxResult.error("当前商品未被添加到反馈列表");
        }
        return AjaxResult.success(dArcItemPicFks);
    }


    /**
     * 更新图片反馈信息
     *
     * @param dArcItemPicFk
     * @return
     */
    @PutMapping("/updateErrorImg")
    @Transactional
    public AjaxResult updateErrorImgFKBySeries(@RequestBody dArcItemPicFk dArcItemPicFk) {
        if (ObjectUtils.isEmpty(dArcItemPicFk)) {
            return AjaxResult.error("参数未被定义.");
        }

        // 首先判断当前图片当前客户是否已经反馈
        dArcItemPicFk fkInfo = dArcItemPicFkService.selectErrorFkInfoByCusIdAndItemIdAndSeries(dArcItemPicFk.getCusCode(), dArcItemPicFk.getItemId(),
                dArcItemPicFk.getSeries());
        // 判断当前对象是否为空 并且反馈信息是否为空
        // 如果不为空，那么就拼接 如果为空，那么就直接更新
        String fkOpinion = null;
        String errorMSG = null;
        if (ObjectUtils.isNotEmpty(fkInfo) && StringUtils.isNotBlank(fkInfo.getFkyj())) {
            // 那么就将客户反馈的值拼接上这次添加的
            fkOpinion = (fkInfo.getFkyj() + "," + dArcItemPicFk.getFkyj());
            dArcItemPicFkService.updateErrImgFkByItemIdAndCusIdAndSeries(dArcItemPicFk.getItemId(),
                    dArcItemPicFk.getCusCode(), dArcItemPicFk.getSeries(), fkOpinion);
            // 根据商品编号获取当前更新图片的人员编号
            dArcItemVo staffIdByItemId = dArcItemService.getStaffIdByItemId(dArcItemPicFk.getItemId());
            String msg = null;
            if (ObjectUtils.isNotEmpty(staffIdByItemId)) {
                dQiyeSendMsg dQiyeSendMsg = new dQiyeSendMsg();
                dQiyeSendMsg.setStaffId(staffIdByItemId.getTeamLeader());
                dQiyeSendMsg.setSeries(dArcItemPicFk.getSeries());
                dQiyeSendMsg.setTypeId("002");
                dQiyeSendMsg.setAgentId("3091828666");
                msg = "当前客户" + dArcItemPicFk.getCusCode() + "对商品编号为【"
                        + dArcItemPicFk.getItemId() + "】的【" + staffIdByItemId.getItemName()
                        + "】的第" + (dArcItemPicFk.getSeries() + 1) + "张图片提出反馈意见：【" + dArcItemPicFk.getFkyj() + "】";
                dQiyeSendMsg.setMsg(msg);
                int effectLine = dQiyeSendMsgService.insertErrorImgFkMSG(dQiyeSendMsg);
                if (effectLine > 0) {
                    dQiyeSendMsg dQiyeSendMsg1 = new dQiyeSendMsg();
                    dQiyeSendMsg1.setStaffId("MS01925");
                    dQiyeSendMsg1.setSeries(dArcItemPicFk.getSeries());
                    dQiyeSendMsg1.setTypeId("002");
                    dQiyeSendMsg1.setAgentId("3091828666");
                    dQiyeSendMsg1.setMsg(msg);
                    int i = dQiyeSendMsgService.insertErrorImgFkMSG(dQiyeSendMsg1);
                    if (i > 0) {
                        dQiyeSendMsg dQiyeSendMsg2 = new dQiyeSendMsg();
                        dQiyeSendMsg2.setStaffId("MS02546");
                        dQiyeSendMsg2.setSeries(dArcItemPicFk.getSeries());
                        dQiyeSendMsg2.setTypeId("002");
                        dQiyeSendMsg2.setAgentId("3091828666");
                        dQiyeSendMsg2.setMsg(msg);
                        int j = dQiyeSendMsgService.insertErrorImgFkMSG(dQiyeSendMsg2);
                        if (j > 0) {
                            errorMSG = "当前客户反馈已提交， 并且已经将反馈发送给图片维护人员，会尽快处理.";
                        }
                    }
                }
            }
        } else {
            fkOpinion = dArcItemPicFk.getFkyj();
            dArcItemPicFkService.updateErrImgFkByItemIdAndCusIdAndSeries(dArcItemPicFk.getItemId(),
                    dArcItemPicFk.getCusCode(), dArcItemPicFk.getSeries(), fkOpinion);
            // 根据商品编号获取当前更新图片的人员编号
            dArcItemVo staffIdByItemId = dArcItemService.getStaffIdByItemId(dArcItemPicFk.getItemId());
            String msg = null;
            if (ObjectUtils.isNotEmpty(staffIdByItemId)) {
                dQiyeSendMsg dQiyeSendMsg = new dQiyeSendMsg();
                dQiyeSendMsg.setStaffId(staffIdByItemId.getTeamLeader());
                dQiyeSendMsg.setSeries(dArcItemPicFk.getSeries());
                dQiyeSendMsg.setTypeId("002");
                dQiyeSendMsg.setAgentId("3091828666");
                msg = "当前客户" + dArcItemPicFk.getCusCode() + "对商品编号为【"
                        + dArcItemPicFk.getItemId() + "】的【" + staffIdByItemId.getItemName()
                        + "】的第" + (dArcItemPicFk.getSeries() + 1) + "张图片提出反馈意见：【" + dArcItemPicFk.getFkyj() + "】";
                dQiyeSendMsg.setMsg(msg);
                int effectLine = dQiyeSendMsgService.insertErrorImgFkMSG(dQiyeSendMsg);
                if (effectLine > 0) {
                    dQiyeSendMsg dQiyeSendMsg1 = new dQiyeSendMsg();
                    dQiyeSendMsg1.setStaffId("MS01925");
                    dQiyeSendMsg1.setSeries(dArcItemPicFk.getSeries());
                    dQiyeSendMsg1.setTypeId("002");
                    dQiyeSendMsg1.setAgentId("3091828666");
                    dQiyeSendMsg1.setMsg(msg);
                    int i = dQiyeSendMsgService.insertErrorImgFkMSG(dQiyeSendMsg1);
                    if (i > 0) {
                        dQiyeSendMsg dQiyeSendMsg2 = new dQiyeSendMsg();
                        dQiyeSendMsg2.setStaffId("MS02546");
                        dQiyeSendMsg2.setSeries(dArcItemPicFk.getSeries());
                        dQiyeSendMsg2.setTypeId("002");
                        dQiyeSendMsg2.setAgentId("3091828666");
                        dQiyeSendMsg2.setMsg(msg);
                        int j = dQiyeSendMsgService.insertErrorImgFkMSG(dQiyeSendMsg2);
                        if (j > 0) {
                            errorMSG = "当前客户反馈已提交， 并且已经将反馈发送给图片维护人员，会尽快处理.";
                        }
                    }
                }
            }
        }

        // 更新完之后获取到最新数据
        List<dArcItemPicFk> dArcItemPicFks = dArcItemPicFkService.selectAllImgFkByCusIdAndItemId(dArcItemPicFk.getCusCode(), dArcItemPicFk.getItemId());

        if (ObjectUtils.isEmpty(dArcItemPicFks)) {
            return AjaxResult.error("Not found customer Fk infos.");
        }
        return AjaxResult.success(errorMSG, dArcItemPicFks);
    }
}
